package system.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Enhancer;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.GET;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.IDataLoader;
import common.info.JsonBackInfo;
import core.jfinal.ext.autoroute.ControllerBind;
import core.jfinal.ext.controller.BaseController;
import net.sf.ehcache.Cache;
import net.sf.ehcache.search.Attribute;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Result;
import net.sf.ehcache.search.Results;
import system.service.AccountSubjectService;

import javax.xml.bind.SchemaOutputResolver;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/6.
 */
@ControllerBind(controllerKey = "/accountSubject")
public class AccountSubjectController extends BaseController{

    AccountSubjectService accountSubjectService = Enhancer.enhance(AccountSubjectService.class);

    public void getAll(){

        List list = CacheKit.get("kjkmCache", "kjkmList", new IDataLoader() {
            @Override
            public Object load() {
                return Db.find("select * from kjkm order by id");
            }
        });

        renderText("getAll");

    }




    @Before({CacheInterceptor.class, GET.class})
    @CacheName("kjkmCache")
    public void getListByCondition() {
        //以访问的路径作为缓存的key,所以必须是get请求

        //模糊查询
        String kmid = getPara("kmid");
        String name = getPara("name");

        List params = new LinkedList();

        StringBuilder sql = new StringBuilder("select * from kjkm where 1=1");
        if(kmid != null && kmid.trim() != ""){
            sql.append(" and kmid like ?");
            params.add(kmid+"%");
        }
        if(null != name && "" != name.trim()){
            sql.append(" and name like ?");
            params.add("%"+name+"%");
        }

        System.out.println(sql.toString());

        List<Record> list = Db.find(sql.toString(), params.toArray());

        renderJson(list);
    }


    public void get(){
        Map accountSubjectMap = accountSubjectService.getAccountSubjectMap();
        accountSubjectMap = (Map) resolveInnerRecord(accountSubjectMap);
        setAttr("accountSubjectMap", accountSubjectMap);

        render("/page/AccountSubject.ftl");
    }

    public void insert(){
        JsonBackInfo jsonBackInfo = new JsonBackInfo();
        Map paraMap = getParaMap();
        System.out.println(JsonKit.toJson(paraMap));
        accountSubjectService.insertAccountSubject(paraMap);

        renderJson(jsonBackInfo.success("添加会计科目成功!"));
    }

    public void update(){
        JsonBackInfo jsonBackInfo = new JsonBackInfo();
        Map paraMap = getParaMap();
        System.out.println(JsonKit.toJson(paraMap));
        accountSubjectService.updateAccountSubject(paraMap);

        renderJson(jsonBackInfo.success("更新会计科目成功"));
    }




}
